﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TerminationCall.Editor.Logic;
using System.Reflection;
using TerminationCall.Editor.Controls;

namespace TerminationCall.Editor.GUI
{
    public partial class ToolsetForm : Form
    {
        private BrushPainterFactory _brushPainterFactory;
        private EraserPainterFactory _eraserPainterFactory;
        private ObjectPainterFactory _objectPainterFactory;

        private PaintToolSet _paintToolSet;

        public ToolsetForm(PaintToolSet paintToolSet)
        {
            InitializeComponent();

            _paintToolSet = paintToolSet;

            _brushPainterFactory = new BrushPainterFactory();
            _eraserPainterFactory = new EraserPainterFactory();
            _objectPainterFactory = new ObjectPainterFactory();
        }

        private void livPaintTools_MouseDown(object sender, MouseEventArgs e) {

            ListViewItem listViewItem = livPaintTools.GetItemAt(e.X, e.Y);
            
            if (listViewItem == null) {
                return;
            }

            IPainterFactory painterFactory = null;

            switch (listViewItem.Text) {

                case "BrushPainter":
                    painterFactory = _brushPainterFactory;
                    break;

                case "Eraser":
                    painterFactory = _eraserPainterFactory;
                    break;

                case "ObjectPainter":
                    painterFactory = _objectPainterFactory;
                    break;


            }

            // Change the Selection of the Painter.
            if (e.X > listViewItem.SubItems[1].Bounds.X) {

                PaintTool paintTool = MouseEventArgsHelper.GetPaintToolByMouseEventArgs(_paintToolSet, e);

                paintTool.PainterFactory = painterFactory;
                _paintToolSet.PaintToolNone.PainterFactory = paintTool.PainterFactory;

                listViewItem.SubItems[1].Text = e.Button.ToString();

            }

            // Setup the Controls for the Properties of the Painter.
            PropertyInfo[] propertyInfos = painterFactory.GetType().GetProperties();

            grpBrushProperties.Controls.Clear();

            int positionX = 20;

            foreach (PropertyInfo propertyInfo in propertyInfos) {

                if (propertyInfo.PropertyType == typeof(int) || propertyInfo.PropertyType == typeof(bool)) {

                    PropertyInfoView propertyInfoView = new PropertyInfoView(propertyInfo, painterFactory);
                    propertyInfoView.Left = 10;
                    propertyInfoView.Top = positionX;
                    grpBrushProperties.Controls.Add(propertyInfoView);
                    propertyInfoView.Visible = true;
                    propertyInfoView.BringToFront();

                    positionX = positionX + 30;

                }

            }

        }
    }
}
